/**
 * @(#)PreloadUtil.java 2011-3-10 Copyright 2011 it.kedacom.com, Inc. All rights
 *                      reserved.
 */

package jaxb.cow;

import java.io.File;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletConfigAware;

/**
 * 获取系统的各种路径,例如web访问根路径,服务器物理路径等.
 * @author 沈俊杰
 * @date 2014-5-15
 */
@Service
public class PathUtils implements ServletConfigAware {
    private static Log log = LogFactory.getLog(PathUtils.class);
    private static boolean debug = true;
    
    private static ServletContext context;
    /** webapp目录的磁盘路径,例如:D:\\Workspace\\EclipseWorkspace\\sales-webapp\\webapp **/
    //private static String WEBAPP_REAL_PATH = "E:\\Project\\Source\\sales\\trunk\\sales-webapp\\webapp";
    private static String WEBAPP_REAL_PATH = "D:\\Workspace\\EclipseWorkspace\\sales-webapp\\webapp";
    private static String WEB_ROOT_URL;//项目的根访问路径
    private static String EXCEL_TEMPLATE_DIR = "export_template";
    
    public void setServletConfig(ServletConfig config)
    {
        if(context == null){
            context = config.getServletContext();
        }
    }
    
    /**
     * <p>获取webapp的磁盘路径</p>
     * <p>例如:E:\sales\trunk\sales-webapp\webapp</p>
     * @param request 请求
     * @return webapp的磁盘路径
     * @author shenjunjie
     * @date [2014-5-16]
     */
    public static String getWebappPath()
    {
        if (WEBAPP_REAL_PATH == null)
        {
            WEBAPP_REAL_PATH = context.getRealPath("/");
        }
        return WEBAPP_REAL_PATH;
    }
    
    /**
     * <p>获取web访问的根路径</p>
     * <p>只需调用一次,以后可以不用传request</p>
     * <p>例如:http://localhost:8080/sales</p>
     * @param request 请求
     * @return http://xxxxxx:8080/sales
     * @author shenjunjie
     * @date [2014-5-16]
     */
    public static String getWebRootURL(HttpServletRequest request)
    {
        if(WEB_ROOT_URL == null && request != null){
            StringBuffer sb = new StringBuffer(request.getScheme()).append("://");
            sb.append(request.getServerName()).append(":");
            sb.append(request.getServerPort());
            sb.append(request.getContextPath());
            WEB_ROOT_URL = sb + "";
        }
        return WEB_ROOT_URL;
    }
    
    public static String getExcelTemplatePath()
    {
        StringBuffer sb = new StringBuffer(getWebappPath());
        sb.append(File.separator).append(EXCEL_TEMPLATE_DIR);
        return sb.toString();
    }
    
    public static void logPath()
    {
        if(debug){
            log.info("@@@@@@@@@@@@@user.dir="+System.getProperty("user.dir"));
            log.info("@@@@@@@@@@@@@getProperties="+System.getProperties());
            log.info("@@@@@@@@@@@@@getRealPath(/)="+context.getRealPath("/"));
            log.info("@@@@@@@@@@@@@getContextPath"+context.getContextPath());
            log.info("@@@@@@@@@@@@@getServerInfo"+context.getServerInfo());
            log.info("@@@@@@@@@@@@@getServletContextName"+context.getServletContextName());
            log.info("@@@@@@@@@@@@@getResourcePaths"+context.getResourcePaths("/"));
        }
    }
    
    public static String getUserDir()
    {
        return System.getProperty("user.dir");
    }

    public String getContextPath()
    {
        return context.getContextPath();
    }

    public String getRealPath(String arg0)
    {
        return context.getRealPath(arg0);
    }

    public Set<?> getResourcePaths(String arg0)
    {
        return context.getResourcePaths(arg0);
    }
    
}
